#include "qcustomserieswidget.h"
#include<QtCharts/QChartView>
#include<QtCharts/QLineSeries>
#include<QtCharts/QCategoryAxis>
#include<QPen>
#include<QColor>
#include<QBrush>

QCustomSeriesWidget::QCustomSeriesWidget(QWidget *parent)
    : QMainWindow{parent}
{

}

void QCustomSeriesWidget::paintEvent(QPaintEvent *event)
{
    QLineSeries* pLineseries = new QLineSeries();
    *pLineseries << QPointF(0,6) << QPointF(9,4)<<QPointF(15,20) << QPointF(25,12)<< QPointF(29,26);
    QChart* pChart = new QChart();
    pChart->legend()->hide();
    pChart->addSeries(pLineseries);

    // 自定义
    QPen pen(QRgb(0xfdb157));
    pen.setWidth(5);
    pLineseries->setPen(pen);

    QFont font;
    font.setPixelSize(18);
    pChart->setTitleFont(font);
    pChart->setTitleBrush(QBrush(Qt::white));
    pChart->setTitle("定义图示例");

    // 定义图表背景
    QLinearGradient backGradient;
    // 设置开始点
    backGradient.setStart(QPointF(0,0));
    backGradient.setFinalStop(QPointF(0,1));
    backGradient.setColorAt(0.0,Qt::green);
    backGradient.setColorAt(1.0,QRgb(0x4c4547));
    // 设置坐标模式
    backGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
    pChart->setBackgroundBrush(QBrush(backGradient));

    // 自定义绘图区域背景
    QLinearGradient poltGradient;
    poltGradient.setStart(QPointF(0,1));
    poltGradient.setFinalStop(QPointF(1,0));
    poltGradient.setColorAt(0.0,QRgb(0x551155));
    poltGradient.setColorAt(1.0,QRgb(0x55aa55));
    poltGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
    // 设置绘图区域背景画笔
    pChart->setPlotAreaBackgroundBrush(poltGradient);
    pChart->setPlotAreaBackgroundVisible(true);

    QCategoryAxis* pAxisX = new QCategoryAxis();
    QCategoryAxis* pAxisY = new QCategoryAxis();

    // 自定义轴标签字体
    QFont lablefont;
    lablefont.setPixelSize(12);
    pAxisX->setLabelsFont(lablefont);
    pAxisY->setLabelsFont(lablefont);

    // 自定义轴颜色
    QPen axisPen;
    axisPen.setWidth(2);
    pAxisX->setLinePen(axisPen);
    pAxisY->setLinePen(axisPen);

     // 自定义轴标签颜色
    QBrush axisBrush(Qt::white);
    pAxisX->setLabelsBrush(axisBrush);
    pAxisY->setLabelsBrush(axisBrush);

    // 自定义网格线和阴影
    pAxisX->setGridLineVisible(true);
    pAxisY->setGridLineVisible(true);
    pAxisY->setShadesPen(Qt::NoPen);
    // 设置用于绘制阴影的画笔以进行比刷
    pAxisY->setShadesBrush(QBrush(QColor(0x99,0xcc,0xcc,0x55)));
    // 设置阴影可见
    pAxisY->setShadesVisible(true);

    pAxisX->append("Low",10);
    pAxisX->append("Optimal",20);
    pAxisX->append("High",30);
    pAxisX->setRange(0,30);

    pAxisY->append("Low",10);
    pAxisY->append("med",20);
    pAxisY->append("fast",30);
    pAxisY->setRange(0,30);
    // 添加轴
    pChart->addAxis(pAxisX,Qt::AlignBottom);
    pChart->addAxis(pAxisY,Qt::AlignLeft);
    // 连接轴
    pLineseries->attachAxis(pAxisX);
    pLineseries->attachAxis(pAxisY);
    // 视图
    QChartView* pView = new QChartView(pChart);
    pView->setRenderHints(QPainter::Antialiasing);
    setCentralWidget(pView);
}
